<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('父级任务')" prop="parentId">
            <el-select v-model="form.parentId" :disabled="form.id !== undefined && form.id !== null && form.id !== ''" :placeholder="$t('父级任务')">
              <el-option :label="$t('无')" value="0" />
              <el-option
                v-for="dict in parentOptions"
                :key="dict.id"
                :label="$t(dict.itemName)"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('事项名称')" prop="itemName">
            <el-input v-model="form.itemName" :placeholder="$t('事项名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input-number v-model="form.sort" controls-position="right" :placeholder="$t('排序')" :precision="0" :max="10000000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:supervise" />
    </div>
  </div>
</template>

<script>
import {
  saveDealSuperviseTemplate,
  getDealSuperviseTemplate,
  listDealSuperviseTemplate
} from '@/api/deal/supervise/dealSuperviseTemplate'
import { getParentNode } from '@/api/act/busFlow/wfBusNode'
    export default {
        props: {
          typeId: {
            type: String,
            required: true,
            default: ''
          }
        },
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  itemName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  parentId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
                parentOptions: []
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data, typeId) {
                this.parentOptions = []
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealSuperviseTemplate(data.id).then(response => {
                        this.form = response.data
                        this.form.typeId = typeId
                        this.getParentSelect(typeId)
                        this.getWorkFlowNode()
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.form.typeId = typeId
                    this.getParentSelect(typeId)
                    this.getWorkFlowNode()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    typeId: undefined,
                    parentId: undefined,
                    itemName: undefined,
                    sort: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealSuperviseTemplate(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          // 查询父级任务
          getParentSelect(typeId) {
            listDealSuperviseTemplate({ parentId: '0', typeId: typeId }).then(response => {
              this.parentOptions = response.rows
              console.log(this.parentOptions)
            })
          },
          // 加载一级下拉
          getWorkFlowNode() {
            getParentNode('deal').then(response => {
              console.log(response)
              this.wordFlowNodeList = response.data
            })
          }
        }
    }
</script>
